using PKGames.Analytics;
using System.Diagnostics;
using UnityEngine;

namespace VoxelGame.Analytics
{
	public class VoxelGameSessionEvent : AnalyticsEvent
	{
		private const string EventName = "game_session";

		private const int EventRevision = 2;

		private const string LevelNameKey = "levelName";

		private const string FarClipPlaneKey = "farClipPlane";

		private const string LoadingTimeKey = "loadTime";

		private const string WorldIdKey = "worldId";

		private const string RamKey = "ram";

		private const string GpuKey = "gpu";

		private const string GpuArcKey = "gpuArc";

		private const string ProcessorCoresKey = "processorCores";

		private const string DeviceModelKey = "deviceModel";

		private const string ElapsedTimeKey = "elapsedTime";

		private const string FlyTimeKey = "flyTime";

		private const string BlocksAddedKey = "blocksAdded";

		private const string BlocksRemovedKey = "blocksRemoved";

		private const string NpcInteractionsKey = "npcInteractions";

		private const string IsTrialKey = "isTrial";

		private readonly Stopwatch _flyModeTime;

		private int _totalAdded;

		private int _totalRemoved;

		private int _totalNpcInteractions;

		private readonly Stopwatch _stopwatch;

		protected override int Flags => 3;

		protected override string Name => "game_session";

		protected override int Revision => 2;

		public VoxelGameSessionEvent(string levelName, bool isTrial)
		{
			_stopwatch = new Stopwatch();
			_stopwatch.Start();
			_flyModeTime = new Stopwatch();
			base.Parameters["levelName"] = new UnityEncodedPrimitiveData(levelName);
			base.Parameters["isTrial"] = new UnityEncodedPrimitiveData(isTrial);
		}

		public void StartFlyTimer()
		{
			_flyModeTime.Start();
		}

		public void StopFlyTimer()
		{
			_flyModeTime.Stop();
		}

		public void BlockAdded()
		{
			_totalAdded++;
		}

		public void IncrementBlocksRemoved(int numberOfBlocksRemoved)
		{
			_totalRemoved += numberOfBlocksRemoved;
		}

		public void OnNpcInteraction()
		{
			_totalNpcInteractions++;
		}

		public void SetFarClipPlane(float farClipPlane)
		{
			base.Parameters["farClipPlane"] = new UnityEncodedPrimitiveData(farClipPlane);
		}

		public void SetLoadingTime(int loadingTime)
		{
			base.Parameters["loadTime"] = new UnityEncodedPrimitiveData((float)loadingTime / 1000f);
		}

		public void SetWorldId(string worldId)
		{
			base.Parameters["worldId"] = new UnityEncodedPrimitiveData(worldId);
		}

		public override void Finish()
		{
			base.Parameters.Add("ram", new UnityEncodedPrimitiveData(SystemInfo.systemMemorySize));
			base.Parameters["gpu"] = new UnityEncodedPrimitiveData(SystemInfo.graphicsDeviceName);
			base.Parameters["gpuArc"] = new UnityEncodedPrimitiveData(SystemInfo.graphicsDeviceVersion);
			base.Parameters["processorCores"] = new UnityEncodedPrimitiveData(SystemInfo.processorCount);
			base.Parameters["deviceModel"] = new UnityEncodedPrimitiveData(SystemInfo.deviceModel);
			_stopwatch.Stop();
			base.Parameters["elapsedTime"] = new UnityEncodedPrimitiveData((float)_stopwatch.ElapsedMilliseconds / 1000f);
			if (_flyModeTime.IsRunning)
			{
				_flyModeTime.Stop();
			}
			base.Parameters["flyTime"] = new UnityEncodedPrimitiveData((float)_flyModeTime.ElapsedMilliseconds / 1000f);
			base.Parameters["blocksAdded"] = new UnityEncodedPrimitiveData(_totalAdded);
			base.Parameters["blocksRemoved"] = new UnityEncodedPrimitiveData(_totalRemoved);
			base.Parameters["npcInteractions"] = new UnityEncodedPrimitiveData(_totalNpcInteractions);
		}
	}
}
